<template>
  <div id="page-modal-container">
    <a-modal
      :visible.sync="visible"
      v-bind="$attrs"
      v-on="$listeners"
      :maskClosable="false"
      :mask="false"
      :closable="false"
      :footer="null"
      :zIndex="4000"
      :getContainer="getContainer"
      @cancel="closeModal()"
      transitionName="fade"
    >
      <div class="system-add-container" v-loading="pageLoading">
        <div class="header" :header="pageTitle">
          <div class="operation">
            <div>
              当前状态：
              <span :class="[checkStatusClass(approvlStatus), 'present']">
                {{ checkStatusName(approvlStatus) }}</span
              >
            </div>
            <a-button v-if="operate == 2" @click="closeModal()">
              返回
            </a-button>
          </div>
        </div>
        <div class="main">
          <!-- 头部流程 -->
          <flowList
            :source="flowSource"
            replaceFields="sysUsername"
            v-if="flowSource.length > 0"
          >
          </flowList>
          <a-form-model ref="form" :model="form" :rules="formRules">
            <custom-collapse :activeKey="activeKey" :panelList="panelList">
              <template slot="space_1">
                <a-row :gutter="20" style="margin-right: 50px">
                  <a-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12" :xxl="6">
                    <a-form-model-item label="抓拍时间">
                      <a-input v-model="form.snapTime" disabled />
                    </a-form-model-item>
                  </a-col>
                  <a-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12" :xxl="6">
                    <a-form-model-item label="处罚类型">
                      <a-input v-model="form.punishType" disabled />
                    </a-form-model-item>
                  </a-col>
                  <a-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" :xxl="24">
                    <a-form-model-item label="处罚内容">
                      <a-input v-model="form.punishContent" disabled />
                    </a-form-model-item>
                  </a-col>
                  <a-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" :xxl="24">
                    <a-form-model-item label="抓拍图片">
                      <div class="picture-img" v-if="form.picture">
                        <img
                          @click.stop="previewImg(form.picture)"
                          :src="form.picture"
                        />
                      </div>
                      <a-input value="暂无图片" disabled v-else />
                    </a-form-model-item>
                  </a-col>
                </a-row>
              </template>
              <template slot="space_2">
                <a-row :gutter="20" style="margin-right: 50px">
                  <a-col :xs="24" :sm="24" :md="24" :lg="12" :xl="8" :xxl="6">
                    <a-form-model-item label="处理人">
                      <a-input
                        type="text"
                        :value="processor || nickName"
                        disabled
                      />
                    </a-form-model-item>
                  </a-col>
                  <a-col :xs="24" :sm="24" :md="24" :lg="12" :xl="8" :xxl="6">
                    <a-form-model-item label="处理时间">
                      <a-input
                        type="text"
                        :value="processTime || time"
                        disabled
                      />
                    </a-form-model-item>
                  </a-col>
                  <a-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" :xxl="24">
                    <a-form-model-item
                      label="处理内容"
                      prop="contentOne"
                      :rules="{
                        required: operate == 0 ? true : false,
                        message: '请输入处理内容',
                        trigger: 'blur',
                      }"
                    >
                      <a-input
                        v-model="form.contentOne"
                        type="textarea"
                        placeholder="请输入处理内容"
                        :maxLength="255"
                        :disabled="operate == 0 ? false : true"
                      />
                    </a-form-model-item>
                  </a-col>
                  <a-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12" :xxl="12">
                    <a-form-model-item
                      label="上传附件"
                      prop="fileList"
                      :rules="{
                        required: false,
                        message: '请选择附件',
                      }"
                    >
                      <bjUploadFile
                        :viewMode="operate == 0 ? false : true"
                        :count="20"
                        v-model="form.fileList"
                        @change="upLoadFile($event)"
                        @download="download($event)"
                        @preview="viewFile($event)"
                        :replaceFields="{
                          fileName: 'realFileName',
                          fileSize: 'fileSize',
                          fileType: 'fileType',
                          fileUrl: 'fileUrl',
                        }"
                      ></bjUploadFile>
                    </a-form-model-item>
                  </a-col>
                </a-row>
              </template>
              <template slot="space_3">
                <a-row :gutter="20" style="margin-right: 50px">
                  <a-col :xs="24" :sm="24" :md="24" :lg="12" :xl="8" :xxl="6">
                    <a-form-model-item label="审批结果" prop="operate">
                      <a-radio-group v-model="form.operate">
                        <a-radio value="2">通过</a-radio>
                        <a-radio value="3">驳回</a-radio>
                      </a-radio-group>
                    </a-form-model-item>
                  </a-col>
                  <a-col :xs="24" :sm="24" :md="24" :lg="12" :xl="8" :xxl="6">
                    <a-form-model-item label="审批人">
                      <a-input type="text" :value="nickName" disabled />
                    </a-form-model-item>
                  </a-col>
                  <a-col :xs="24" :sm="24" :md="24" :lg="12" :xl="8" :xxl="6">
                    <a-form-model-item label="审批时间">
                      <a-input type="text" :value="time" disabled />
                    </a-form-model-item>
                  </a-col>
                  <a-col :xs="24" :sm="24" :md="24">
                    <a-form-model-item label="审批意见" prop="reason">
                      <a-input
                        type="textarea"
                        v-model="form.reason"
                        placeholder="请输入审批意见"
                        :maxLength="250"
                      />
                    </a-form-model-item>
                  </a-col>
                </a-row>
              </template>
              <template slot="space_5">
                <div class="approval-record">
                  <div
                    class="record-item"
                    v-for="(item, index) in dotList"
                    :key="index"
                  >
                    <div class="time">{{ item.updateTimeStr }}</div>
                    <div class="dotsList">
                      <div class="link-line">
                        <div
                          class="dots"
                          :class="0 == index ? 'activied' : ''"
                        ></div>
                        <div
                          :class="index != dotList.length - 1 ? 'lines' : ''"
                        ></div>
                      </div>
                    </div>
                    <div class="content">
                      <div class="name">
                        <span class="user">
                          {{ item.userName }}
                        </span>
                        <span class="explain">
                          {{ item.wfnodename }}
                        </span>
                      </div>
                      <div class="reply">
                        {{ item.reply || item.reason }}
                      </div>
                    </div>
                  </div>
                </div>
              </template>
            </custom-collapse>
          </a-form-model>
        </div>
        <div class="footer" v-if="operate != 2">
          <a-button @click="closeModal()">取消 </a-button>
          <a-button
            type="primary"
            @click="approvalSave()"
            :loading="buttonLoading"
          >
            确定
          </a-button>
        </div>
      </div>
    </a-modal>
    <!-- 预览 -->
    <PreviewModel :visible.sync="previewVisible" :imgUrl="path"></PreviewModel>
  </div>
</template>

<script>
import flowList from "@/components/flow-list";
import bjUploadFile from "@/components/bj-upload-file";
import PreviewModel from "@/components/PreviewModel";
import {
  getSnapDetail,
  getSnapRecord,
  getSnapApprove,
  snapUploadFile,
  filePath,
} from "@/api/wisdom-beam/security-app.js";
import {
  getNowDateStr,
  downFileByUrl,
  notificationTip,
  commonViewFile,
} from "@/utils";
export default {
  name: "customModal",
  components: {
    flowList,
    bjUploadFile,
    PreviewModel,
  },
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    parameter: {
      type: Object,
      default: {},
    },
  },
  watch: {
    visible: {
      handler(val) {
        this.$emit("update:visible", val);
        if (val) {
          let { operate } = this.parameter;
          this.operate = operate;
          this.getData();
        }
      },
      immediate: true,
    },
  },
  data() {
    return {
      //0处理 1审批 2查看
      operate: "",
      pageTitle: "查看",
      //折叠面板
      activeKey: [],
      panelList: [],
      //审批人/时间
      nickName: "",
      time: "",
      //处理人/处理时间
      processor: "",
      processTime: "",
      //form表单
      form: {
        //抓拍时间
        snapTime: "",
        //人员类别
        personCategory: "",
        //惩罚类型
        punishType: "",
        //惩罚内容
        punishContent: "",
        //抓拍图片
        picture: "",
        //处理内容
        contentOne: "",
        //审批结果
        operate: "",
        //审批意见
        reason: "",
        //附件
        fileList: [],
      },
      formRules: {
        operate: [
          {
            required: true,
            message: "请选择审批结果",
            trigger: "change",
          },
        ],
      },
      pageLoading: false,
      buttonLoading: false,
      //预览
      previewVisible: false,
      path: "",
      //审批记录
      dotList: [],
      //头部流程
      flowSource: [],
      approvlStatus: "",
    };
  },

  mounted() {},
  methods: {
    //预览图片
    previewImg(url) {
      this.path = url;
      this.previewVisible = true;
    },
    getData() {
      this.pageTitle =
        this.operate == 0 ? "处理" : this.operate == 1 ? "审批" : "查看";
      switch (this.operate) {
        case 0:
          this.activeKey = ["1", "2"];
          this.panelList = [
            {
              key: "1",
              header: "抓拍信息",
            },
            {
              key: "2",
              header: "处理信息",
            },
          ];
          break;
        case 1:
          this.activeKey = ["1", "2", "3"];
          this.panelList = [
            {
              key: "1",
              header: "抓拍信息",
            },
            {
              key: "2",
              header: "处理信息",
            },
            {
              key: "3",
              header: "审批信息",
            },
          ];
          break;
        case 2:
          this.activeKey = ["1", "2", "5"];
          this.panelList = [
            {
              key: "1",
              header: "抓拍信息",
            },
            {
              key: "2",
              header: "处理信息",
            },
            {
              key: "5",
              header: "审批记录",
            },
          ];
          break;
      }
      this.time = getNowDateStr(true);
      this.nickName = JSON.parse(
        localStorage.getItem("LOGIN_DATA")
      ).userInfo.nickName;
      Promise.all([this.getDetail(), this.getRecord()]).then((res) => {});
    },
    //审批
    approvalSave() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          this.buttonLoading = true;
          let { operate, form } = this;
          let { id } = this.parameter;
          let formJson = {};
          //处理
          if (operate == 0) {
            let files = form?.fileList || [];
            formJson = {
              id,
              operate: "2",
              contentOne: form.contentOne,
              attachment: files.map((v) => v.fileId).toString(),
            };
          }
          //审批
          if (operate == 1) {
            formJson = {
              id,
              operate: form.operate,
              reason: form.reason,
            };
          }
          getSnapApprove(formJson)
            .then((res) => {
              notificationTip(
                "success",
                operate == 0 ? "处理成功" : "审批成功"
              );
              this.$parent.getData();
              this.closeModal();
            })
            .catch((_) => {})
            .finally((_) => {
              this.buttonLoading = false;
            });
        }
      });
    },

    //查询详情
    getDetail() {
      this.pageLoading = true;
      return new Promise((resolve, reject) => {
        let { id } = this.parameter;
        getSnapDetail(id)
          .then((res) => {
            let {
              data: { result },
            } = res;
            this.approvlStatus = result.approvlStatus;
            this.form.snapTime = result.snapTime;
            this.form.personCategory = result.personCategory;
            this.form.punishType = result.punishType;
            this.form.punishContent = result.punishContent;
            this.form.picture = result.picture;
            this.form.contentOne = result.contentOne;
            this.form.fileList = result.attachmentFiles || [];
            this.processor = result?.processor || "";
            this.processTime = result?.processTime || "";
            resolve();
          })
          .catch((_) => {
            reject();
          })
          .finally((_) => {
            this.pageLoading = false;
          });
      });
    },
    //查询审批记录
    getRecord() {
      return new Promise((resolve, reject) => {
        let { id } = this.parameter;
        getSnapRecord(id)
          .then((res) => {
            let { data } = res;
            this.dotList = data?.result || [];
            //顶部流程条
            let flowList = data?.result || [];
            if (flowList.length > 0) {
              flowList.forEach((v, i) => {
                let { wfnodename, operation } = v;
                v.subTitle = wfnodename;
                if (operation == "0" || operation == "2") {
                  v.$$nodeStatus = "active";
                }
              });
            }
            this.flowSource = flowList;
            resolve();
          })
          .catch((_) => {
            this.flowSource = [];
            this.dotList = [];
            reject();
          });
      });
    },

    //关闭页面
    closeModal() {
      this.$refs.form.resetFields();
      this.form = {
        //抓拍时间
        snapTime: "",
        //人员类别
        personCategory: "",
        //惩罚类型
        punishType: "",
        //惩罚内容
        punishContent: "",
        //抓拍图片
        picture: "",
        //处理内容
        contentOne: "",
        //审批结果
        operate: "",
        //审批意见
        reason: "",
        //附件
        fileList: [],
      };
      this.$emit("update:visible", false);
    },

    //上传附件
    upLoadFile({ file, index }) {
      let formData = new FormData();
      formData.append("uploadFiles", file);
      this.$set(this.form["fileList"][index], "loading", true);
      return new Promise((resolve, reject) => {
        snapUploadFile(formData)
          .then((res) => {
            let { data } = res;
            let fileId = data?.result?.successFileId?.[0] || "";
            this.$set(this.form["fileList"], index, {
              ...this.form["fileList"][index],
              fileId,
            });
            resolve();
          })
          .catch((_) => {
            this.$set(this.form["fileList"][index], "fail", true);
            reject();
          })
          .finally((_) => {
            this.$set(this.form["fileList"][index], "loading", false);
          });
      });
    },

    //下载附件
    download(file) {
      let { realFileName, id } = file;
      filePath(id)
        .then((res) => {
          let fileUrl = res.data?.result || "";
          if (fileUrl)
            downFileByUrl(fileUrl + "&attname=" + realFileName, realFileName);
        })
        .catch((_) => {});
    },

    //附件预览
    viewFile(file) {
      let { id } = file;
      commonViewFile("beam", id);
    },

    checkStatusName(state) {
      switch (state) {
        case "0":
          return "未处理";

        case "1":
          return "处理中";

        case "2":
          return "已完成";

        default:
          return "- -";
      }
    },
    checkStatusClass(state) {
      switch (state) {
        case "0":
          return "error";

        case "1":
          return "warn";

        case "2":
          return "normal";

        default:
          return "cancel";
      }
    },

    getContainer() {
      return document.getElementById("page-modal-container");
    },
  },
};
</script>

<style lang="less" scoped>
.system-add-container {
  height: 100%;
  padding: 10px 10px 10px 10px;
  .flex();
  flex-direction: column;
  .header {
    padding: 15px 20px;
    background-color: #fff;
    box-sizing: border-box;
    .flex();
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid #f3f4f6;
    &::before {
      content: attr(header);
      height: 18px;
      border-left: 3px solid var(--primary-color);
      padding-left: 10px;
      font-size: 18px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      color: #525a5d;
      line-height: 18px;
    }
    .operation {
      flex: 1;
      .flex();
      justify-content: space-between;
      align-items: center;
      padding-left: 10px;
      .present {
        border-radius: 4px;
        font-size: 14px;
        font-weight: 400;
        padding: 0 8px;
        text-align: center;
        display: inline-block;
        line-height: 22px;
        color: #fff;
        &.error {
          background: #ff3f4a;
        }
        &.warn {
          background: #ff993f;
        }
        &.normal {
          background: #4bc964;
        }
        &.cancel {
          background: #f1f2f7;
          color: #aab0bc;
        }
      }
    }
  }
  .main {
    background-color: #fff;
    flex: 1;
    height: 100%;
    .custom-scrollbar();
    .flex();
    flex-direction: column;
    //上传图片
    .picture-img {
      display: flex;
      flex-wrap: wrap;
      img {
        width: 128px;
        height: 122px;
        border-radius: 2px;
        object-fit: contain;
        background-color: #f3f4f6;
      }
    }
    //审批记录
    .approval-record {
      padding: 0 50px 0 66px;
      .record-item {
        .flex();
        .time {
          min-width: 100px;
          font-size: 14px;
          font-family: Microsoft YaHei;
          font-weight: 400;
          color: #333333;
          line-height: 22px;
        }
        .dotsList {
          .flex();
          flex-direction: column;
          margin: 3px 10px 0 10px;
          .link-line {
            .flex();
            flex-direction: column;
            align-items: center;
            flex: 1;
            .dots {
              .flex();
              flex-direction: column;
              justify-content: center;
              align-items: center;
              width: 13px;
              height: 13px;
              border-radius: 50%;
              background-color: #fff;
              border: 2px solid #d9dce2;
            }
            .activied {
              background-color: #fff;
              border: 2px solid #d9dce2;
              width: 13px;
              height: 13px;
            }
            .lines {
              width: 1px;
              flex: 1;
              min-height: 50px;
              background-color: #d9dce2;
              margin-top: 3px;
            }
          }
        }
        .content {
          flex: 1;
          .name {
            margin-bottom: 10px;
            .user {
              font-size: 16px;
              font-family: Microsoft YaHei;
              font-weight: 400;
              color: #333333;
              line-height: 22px;
            }
            .explain {
              font-size: 14px;
              font-family: Microsoft YaHei;
              font-weight: 400;
              color: #666666;
              line-height: 22px;
            }
          }
          .reply {
            background: #f7f7f7;
            border-radius: 4px;
            padding: 0px 6px;
            margin-bottom: 50px;
            font-size: 14px;
            font-family: Microsoft YaHei;
            font-weight: 400;
            color: #333333;
            line-height: 30px;
          }
        }
      }
    }
  }
  .footer {
    border-top: 1px solid #f3f4f6;
    background-color: #fff;
    .flex();
    align-items: center;
    justify-content: center;
    padding: 20px 0;
  }
  //表单布局
  /deep/.ant-form-item {
    display: flex;
    .ant-form-item-label {
      width: 130px;
      min-width: 130px;
      white-space: pre-wrap;
      display: flex;
      justify-content: flex-end;
      line-height: 20px;
      align-items: center;
      height: 36px;
      label {
        color: #999999;
      }
    }
    .check-item-select {
      height: 126px;
      .ant-select-selection__rendered {
        height: 126px;
        .ant-select-selection__placeholder {
          margin-top: -40px !important;
        }
      }
    }
    .ant-select {
      width: 100% !important;
      min-width: unset !important;
    }
    .ant-form-item-control-wrapper {
      flex: 1;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
  }
}

/deep/.ant-modal-root {
  .ant-modal-wrap {
    position: fixed;
    top: 64px;
    left: 0px;
    bottom: 0;
    right: 0;
    .ant-modal {
      width: calc(100vw - 0px) !important;
      top: 0;
      padding-bottom: 0;
      margin: 0;
      .ant-modal-content {
        background-color: #f3f4f6;
        box-shadow: none;
        .ant-modal-body {
          padding: 0;
          height: calc(100vh - 64px) !important;
          overflow: hidden;
        }
      }
    }
  }
}
</style>
